\hypertarget{class_missile}{
\section{Missile Class Reference}
\label{class_missile}\index{Missile@{Missile}}
}


{\ttfamily \hyperlink{class_missile}{Missile}} class to implement missiles fired by UFO's derived from {\ttfamily \hyperlink{class_game_object}{GameObject}} and {\ttfamily \hyperlink{class_rotating_object}{RotatingObject}}.  


Inheritance diagram for Missile:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[height=2.000000cm]{class_missile}
\end{center}
\end{figure}
\subsection*{Public Member Functions}
\begin{DoxyCompactItemize}
\item 
\hyperlink{class_missile_a6d55f49b6df0137a180e52965af3c6f7}{Missile} (int bornFrame, bool rewindEnabled, FPtype guidanceFactor, FPtype speedFactor)
\begin{DoxyCompactList}\small\item\em Constructor for \hyperlink{class_missile}{Missile} class. \item\end{DoxyCompactList}\item 
void \hyperlink{class_missile_abde8aa0cb2b72a8035bfb05a59ea983f}{initDirection} (const FPtype $\ast$dir)
\begin{DoxyCompactList}\small\item\em Initializes the missile direction according to given direction vector. \item\end{DoxyCompactList}\item 
void \hyperlink{class_missile_ac0c69a02cb31c898487b3f9954ea7d21}{adjustDirection} (const FPtype $\ast$dir)
\begin{DoxyCompactList}\small\item\em Adjusts the missile direction according to given direction vector. \item\end{DoxyCompactList}\item 
void \hyperlink{class_missile_a7ee8ac315cfcab51ca9a66de7d7292d2}{initRewindDirection} (int frame)
\begin{DoxyCompactList}\small\item\em Initialize the missile direction in rewind mode. \item\end{DoxyCompactList}\item 
void \hyperlink{class_missile_aa99853cb58c0f05fd8de7bd05f42305d}{adjustRewindDirection} (int frame)
\begin{DoxyCompactList}\small\item\em Adjust the missile direction in rewind mode. \item\end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Private Member Functions}
\begin{DoxyCompactItemize}
\item 
void \hyperlink{class_missile_a50e3a9e2277dca2fa4cb1e2fc75861f0}{setDirection} (const FPtype $\ast$dir, bool reset=false)
\begin{DoxyCompactList}\small\item\em Sets the missile direction according to given direction vector. \item\end{DoxyCompactList}\item 
void \hyperlink{class_missile_aa96bb593a54fdabc62fae8f890c03ead}{setRewindDirection} (int frame, bool reset=false)
\begin{DoxyCompactList}\small\item\em Set the missile direction in rewind mode. \item\end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Detailed Description}
{\ttfamily \hyperlink{class_missile}{Missile}} class to implement missiles fired by UFO's derived from {\ttfamily \hyperlink{class_game_object}{GameObject}} and {\ttfamily \hyperlink{class_rotating_object}{RotatingObject}}. The missile direction angle is implemented using the properties form {\ttfamily \hyperlink{class_rotating_object}{RotatingObject}}. 

\subsection{Constructor \& Destructor Documentation}
\hypertarget{class_missile_a6d55f49b6df0137a180e52965af3c6f7}{
\index{Missile@{Missile}!Missile@{Missile}}
\index{Missile@{Missile}!Missile@{Missile}}
\subsubsection[{Missile}]{\setlength{\rightskip}{0pt plus 5cm}Missile::Missile (
\begin{DoxyParamCaption}
\item[{int}]{ bornFrame, }
\item[{bool}]{ rewindEnabled, }
\item[{FPtype}]{ guidanceFactor, }
\item[{FPtype}]{ speedFactor}
\end{DoxyParamCaption}
)}}
\label{class_missile_a6d55f49b6df0137a180e52965af3c6f7}


Constructor for \hyperlink{class_missile}{Missile} class. 


\begin{DoxyParams}{Parameters}
\item[{\em bornFrame}]Current game frame number when game object was born. \item[{\em rewindEnabled}]Is the rewind mode playback enabled. \item[{\em guidanceFactor}]Guidance factor of the missile, how steep is the guidance towards target (miner). \item[{\em speedFactor}]Speed factor of the missile, how fast is the missile movement. \end{DoxyParams}


\subsection{Member Function Documentation}
\hypertarget{class_missile_ac0c69a02cb31c898487b3f9954ea7d21}{
\index{Missile@{Missile}!adjustDirection@{adjustDirection}}
\index{adjustDirection@{adjustDirection}!Missile@{Missile}}
\subsubsection[{adjustDirection}]{\setlength{\rightskip}{0pt plus 5cm}void Missile::adjustDirection (
\begin{DoxyParamCaption}
\item[{const FPtype $\ast$}]{ dir}
\end{DoxyParamCaption}
)}}
\label{class_missile_ac0c69a02cb31c898487b3f9954ea7d21}


Adjusts the missile direction according to given direction vector. 

\hyperlink{class_missile}{Missile} direction is set using the {\ttfamily \hyperlink{class_missile_a50e3a9e2277dca2fa4cb1e2fc75861f0}{Missile::setDirection()}} method.


\begin{DoxyParams}{Parameters}
\item[{\em dir}]Direction vector with X-\/ and Y-\/components in indexes 0 and 1. \end{DoxyParams}
\hypertarget{class_missile_aa99853cb58c0f05fd8de7bd05f42305d}{
\index{Missile@{Missile}!adjustRewindDirection@{adjustRewindDirection}}
\index{adjustRewindDirection@{adjustRewindDirection}!Missile@{Missile}}
\subsubsection[{adjustRewindDirection}]{\setlength{\rightskip}{0pt plus 5cm}void Missile::adjustRewindDirection (
\begin{DoxyParamCaption}
\item[{int}]{ frame}
\end{DoxyParamCaption}
)}}
\label{class_missile_aa99853cb58c0f05fd8de7bd05f42305d}


Adjust the missile direction in rewind mode. 


\begin{DoxyParams}{Parameters}
\item[{\em frame}]Current frame in rewind mode playback. \end{DoxyParams}
\hypertarget{class_missile_abde8aa0cb2b72a8035bfb05a59ea983f}{
\index{Missile@{Missile}!initDirection@{initDirection}}
\index{initDirection@{initDirection}!Missile@{Missile}}
\subsubsection[{initDirection}]{\setlength{\rightskip}{0pt plus 5cm}void Missile::initDirection (
\begin{DoxyParamCaption}
\item[{const FPtype $\ast$}]{ dir}
\end{DoxyParamCaption}
)}}
\label{class_missile_abde8aa0cb2b72a8035bfb05a59ea983f}


Initializes the missile direction according to given direction vector. 

\hyperlink{class_missile}{Missile} direction is set using the {\ttfamily \hyperlink{class_missile_a50e3a9e2277dca2fa4cb1e2fc75861f0}{Missile::setDirection()}} method.


\begin{DoxyParams}{Parameters}
\item[{\em dir}]Direction vector with X-\/ and Y-\/components in indexes 0 and 1. \end{DoxyParams}
\hypertarget{class_missile_a7ee8ac315cfcab51ca9a66de7d7292d2}{
\index{Missile@{Missile}!initRewindDirection@{initRewindDirection}}
\index{initRewindDirection@{initRewindDirection}!Missile@{Missile}}
\subsubsection[{initRewindDirection}]{\setlength{\rightskip}{0pt plus 5cm}void Missile::initRewindDirection (
\begin{DoxyParamCaption}
\item[{int}]{ frame}
\end{DoxyParamCaption}
)}}
\label{class_missile_a7ee8ac315cfcab51ca9a66de7d7292d2}


Initialize the missile direction in rewind mode. 


\begin{DoxyParams}{Parameters}
\item[{\em frame}]Current frame in rewind mode playback. \end{DoxyParams}
\hypertarget{class_missile_a50e3a9e2277dca2fa4cb1e2fc75861f0}{
\index{Missile@{Missile}!setDirection@{setDirection}}
\index{setDirection@{setDirection}!Missile@{Missile}}
\subsubsection[{setDirection}]{\setlength{\rightskip}{0pt plus 5cm}void Missile::setDirection (
\begin{DoxyParamCaption}
\item[{const FPtype $\ast$}]{ dir, }
\item[{bool}]{ reset = {\ttfamily false}}
\end{DoxyParamCaption}
)\hspace{0.3cm}{\ttfamily  \mbox{[}private\mbox{]}}}}
\label{class_missile_a50e3a9e2277dca2fa4cb1e2fc75861f0}


Sets the missile direction according to given direction vector. 

Initializes the missile direction if reset is requested.

Direction vector is a vector from missile to miner. The distance between missile and miner is calculated according to pythagorean theorem from X-\/ and Y-\/components $c = \sqrt{a^2 + b^2}$.


\begin{DoxyCode}
 dist = sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
\end{DoxyCode}


The direction vector is normalized into a unit vector by dividing the components with distance:


\begin{DoxyCode}
 dirXNorm = dir[0] / dist;
 dirYNorm = dir[1] / dist;
\end{DoxyCode}


Since the unit vector is created by dividing with distance, it's important to check that the distance is not zero or too close to zero. If the distance is under 0.1 we just set the rotation angle to 0.0.

If the reset was not requested the missile direction will be adjusted according to given direction and guidance factor. Unit vector for current velocity is calculaated:


\begin{DoxyCode}
 currVelDist = sqrt(currVelX * currVelX + currVelY * currVelY);
 currVelXNorm = currVelX / currVelDist;
 currVelYNorm = currVelY / currVelDist;
\end{DoxyCode}


Guidance vector is calculated using distance vector normal and guidance. 
\begin{DoxyCode}
 guidX = (guidance / 20.0f) * dirXNorm;
 guidY = (guidance / 20.0f) * dirYNorm;
\end{DoxyCode}


Adjusted direction vector is summed from current velocity unit vector and guidance vector: 
\begin{DoxyCode}
 fixDirX = currVelXNorm + guidX;
 fixDirY = currVelYNorm + guidY;
\end{DoxyCode}


A unit vector for adjusted direction is calculated: 
\begin{DoxyCode}
 fixVelDist = sqrt(fixDirX * fixDirX + fixDirY * fixDirY);
 dirXNorm = fixDirX / fixVelDist;
 dirYNorm = fixDirY / fixVelDist;
\end{DoxyCode}


Rotation angle according to direction vector is calculated always. Direction vector is either adjusted direction vector as described above or if reset was requested, an unit vector calculated from given direction vector.


\begin{DoxyItemize}
\item Angle between two vectors can be calculated from
\end{DoxyItemize}

$ A \cdot B = |A| * |B| * cos(\theta) $


\begin{DoxyItemize}
\item Angle between two \_\-unit\_\- vectors can be calculated from
\end{DoxyItemize}

$ A \cdot B = 1 * 1 * cost(\theta) $


\begin{DoxyItemize}
\item so
\end{DoxyItemize}

$ \theta = acos(A \cdot B) $

Dot product: $ A \cdot B = A_1B_1 + \cdots + A_nB_n $

\hyperlink{class_missile}{Missile} default direction (rotation angle 0.0) is along the Y-\/axis, tail in origo. So the dot product between y-\/axis (the initial direction of missile is along the y-\/axis) and the direction vector is calculated.


\begin{DoxyCode}
 dirXNorm * 0 + dirYNorm * 1
\end{DoxyCode}


So the dot product equals to {\ttfamily dirYNorm}.

If {\ttfamily dirXNorm} is near zero or zero and {\ttfamily dirYNorm} is positive the angle is the default angle. If {\ttfamily dirYNorm} is negative the angle is rotated 180°.

If {\ttfamily dirYNorm} is near zero or zero and {\ttfamily dirXNorm} is positive the rotation angle is -\/90°. If {\ttfamily dirXNorm} is negative the rotation angle is 90°.

Otherwise the angle between y-\/axis and direction vector is calculated.


\begin{DoxyCode}
 a = acos(dirYNorm) * 180.0f / M_PI;
\end{DoxyCode}


The missile rotation angle may still need to be fixed -\/-\/-\/ the possible values for {\ttfamily a} are from -\/90° to 90°:
\begin{DoxyItemize}
\item when a $<$ 0 the angle is $>$ 90°
\item when a $>$ 0 the angle is $<$ 90°
\end{DoxyItemize}

When {\ttfamily a} $>$ 0.0f the angle is $<$ 90° and the rotation direction is correct, otherwise when {\ttfamily dirXNorm} $>$ 0.0 the rotation direction must be set to negative. When {\ttfamily a} $<$ 0.0f the angle is $>$ 90°, also the rotation direction is fixed when {\ttfamily dirXNorm} $>$ 0.0:


\begin{DoxyCode}
 if (a > 0.0f && dirXNorm > 0.0f)
      a = -a;
 else if (a < 0.0f && dirXNorm > 0.0f)
      a = -90.0f + a;
 else if (a < 0.0f && dirXNorm < 0.0f)
      a = 90.0f - a;
\end{DoxyCode}



\begin{DoxyParams}{Parameters}
\item[{\em dir}]Direction vector with X-\/ and Y-\/components in indexes 0 and 1. \item[{\em reset}]If set to ''true'' the missile direction is initialized otherwise the missile direction is adjusted. \end{DoxyParams}
\hypertarget{class_missile_aa96bb593a54fdabc62fae8f890c03ead}{
\index{Missile@{Missile}!setRewindDirection@{setRewindDirection}}
\index{setRewindDirection@{setRewindDirection}!Missile@{Missile}}
\subsubsection[{setRewindDirection}]{\setlength{\rightskip}{0pt plus 5cm}void Missile::setRewindDirection (
\begin{DoxyParamCaption}
\item[{int}]{ frame, }
\item[{bool}]{ reset = {\ttfamily false}}
\end{DoxyParamCaption}
)\hspace{0.3cm}{\ttfamily  \mbox{[}private\mbox{]}}}}
\label{class_missile_aa96bb593a54fdabc62fae8f890c03ead}


Set the missile direction in rewind mode. 

The missile direction in rewind mode playback is calculated using a direction vector calculated from missile location in current frame and previous frame. The direction is set using {\ttfamily \hyperlink{class_missile_a50e3a9e2277dca2fa4cb1e2fc75861f0}{Missile::setDirection()}} accordingly.


\begin{DoxyParams}{Parameters}
\item[{\em frame}]Current frame in rewind mode playback. \item[{\em reset}]If set to ''true'' the missile direction is initialized otherwise the missile direction is adjusted. \end{DoxyParams}


The documentation for this class was generated from the following files:\begin{DoxyCompactItemize}
\item 
\hyperlink{missile_8h}{missile.h}\item 
missile.cpp\end{DoxyCompactItemize}
